<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Support\Facades\Hash;
class CateController extends Controller{
	public function __construct()
    {
        $this->middleware('admin');
    }
    public function cate()
    {
		return view('admin.cate.cate');
    }
    public function cates()
    {
        $cates = DB::table('cate')->select('id','name')->where('p_id','0')->get();
        $catess=$this->subcate($cates);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'data'  => $catess
        ];
        return response()->json($data);
    }
    private function subcate($cates){
        for ($i=0; $i <count($cates) ; $i++) {
            $subcates = DB::table('cate')->select('id','name')->where('p_id',$cates[$i]->id)->get();
            if($subcates){
                if(count($subcates)>0){
                    $cates[$i]->children=$subcates;
                    $this->subcate($subcates);
                }
            }
        }
        return $cates;
    }
    public function addcate(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('cate')->insert(['name' => $request->name,'p_id' =>0,'sort' =>1,'state' =>1]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
        return view('admin.cate.addcate');
    }
    public function addsubcate(Request $request)
    {
        $res=DB::table('cate')->where('id',$request->id)->first();
        $id=$request->id;
        $name=$res->name;
        return view('admin.cate.addsubcate', compact('id','name'));
    }
    public function addsubcateup(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('cate')->insert(['name' => $request->name,'p_id' =>$request->p_id,'sort' =>1,'state' =>1]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
    }
    public function editcate(Request $request)
    {
        $res=DB::table('cate')->where('id',$request->id)->first();
        if($res->p_id>0){
            $fa=DB::table('cate')->where('id',$res->p_id)->first();
            $fname=$fa->name;
        }else{
            $fname=0;
        }
        $id=$request->id;
        $name=$res->name;
        return view('admin.cate.editcate', compact('id','fname','name'));
    }
    public function editcateup(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('cate')->where('id',$request->id)->update(['name' => $request->name]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
    }
    public function delcate(Request $request)
    {
            $res=DB::table('cate')->where('id',$request->id)->delete();
            $this->delsubcate($request->id);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    private function delsubcate($id){
        $subs=DB::table('cate')->where('p_id',$id)->get();
        for ($i=0; $i <count($subs) ; $i++) { 
            DB::table('cate')->where('id',$subs[$i]->id)->delete();
            $this->delsubcate($subs[$i]->id);
        }
    }
}